﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kokomo.Environment.Services;
using Microsoft.Win32;

namespace Kokomo.Environment.Hosting.Services
{
	class UiService : IUiService
	{
		public FileDialogResult ShowOpenFileDialog(FileDialogOptions options)
		{
			if (options == null) throw new ArgumentNullException("options");

			StringBuilder filterBuilder = new StringBuilder();
			List<string> allMasks = new List<string>();
			int fileTypeCount = 0;
			if (options.FileTypes != null)
			{
				foreach (var fileType in options.FileTypes)
				{
					filterBuilder.Append(fileType.Description);
					filterBuilder.Append(" (");
					filterBuilder.Append(string.Join(", ", fileType.Masks));
					filterBuilder.Append(")|");
					filterBuilder.Append(string.Join(";", fileType.Masks));
					filterBuilder.Append("|");

					foreach (var mask in fileType.Masks)
					{
						if (!allMasks.Contains(mask, StringComparer.InvariantCultureIgnoreCase)) allMasks.Add(mask);
					}
				}
				filterBuilder.Append(Messages.AllDocumentsText + " (");
				filterBuilder.Append(string.Join(", ", allMasks));
				filterBuilder.Append(")|");
				filterBuilder.Append(string.Join(";", allMasks));
				filterBuilder.Append("|");

				fileTypeCount = options.FileTypes.Count();
			}
			filterBuilder.Append(Messages.AllFilesText + " (*.*)|*.*");

			string filter = filterBuilder.ToString();

			OpenFileDialog openFileDialog = new OpenFileDialog()
			{
				Title = options.Title,
				Filter = filter,
				FilterIndex = fileTypeCount + 1,
				Multiselect = true,
				ShowReadOnly = true,
				InitialDirectory = options.InitialDirectory,
				FileName = options.InitialFileName
			};

			bool result = openFileDialog.ShowDialog().Value;
			if (result) return new FileDialogResult(openFileDialog.FileNames);
			else return new FileDialogResult(null);
		}
		public string ShowSaveFileDialog(FileDialogOptions options)
		{
			if (options == null) throw new ArgumentNullException("options");

			StringBuilder filterBuilder = new StringBuilder();
			List<string> allMasks = new List<string>();
			int fileTypeCount = 0;
			if (options.FileTypes != null)
			{
				foreach (var fileType in options.FileTypes)
				{
					filterBuilder.Append(fileType.Description);
					filterBuilder.Append(" (");
					filterBuilder.Append(string.Join(", ", fileType.Masks));
					filterBuilder.Append(")|");
					filterBuilder.Append(string.Join(";", fileType.Masks));
					filterBuilder.Append("|");

					foreach (var mask in fileType.Masks)
					{
						if (!allMasks.Contains(mask, StringComparer.InvariantCultureIgnoreCase)) allMasks.Add(mask);
					}
				}

				fileTypeCount = options.FileTypes.Count;
			}
			filterBuilder.Append("All files (*.*)|*.*");

			string filter = filterBuilder.ToString();

			string defaultExtension = (fileTypeCount == 0) ? null : options.FileTypes.First().DefaultExtension;

			SaveFileDialog saveFileDialog = new SaveFileDialog()
			{
				Title = options.Title,
				Filter = filter,
				FileName = options.InitialFileName,
				DefaultExt = defaultExtension
			};

			bool result = saveFileDialog.ShowDialog().Value;

			if (result)
			{
				int filterIndex = saveFileDialog.FilterIndex;
				if (filterIndex >= 1 && (options.FileTypes != null) && filterIndex <= options.FileTypes.Count)
					options.SelectedFileType = options.FileTypes[filterIndex - 1];

				return saveFileDialog.FileName;
			}
			return null;
		}
	}
}
